package com.zhenzhang.myblog.controller;

import com.zhenzhang.myblog.annotations.Authentication;
import com.zhenzhang.myblog.dao.ArticleDao;
import com.zhenzhang.myblog.entity.Article;
import com.zhenzhang.myblog.entity.MultiPageInfo;
import com.zhenzhang.myblog.entity.role.UserRole;
import com.zhenzhang.myblog.exception.JsonException;
import com.zhenzhang.myblog.service.ArticleService;
import com.zhenzhang.myblog.utils.FileUtils;
import com.zhenzhang.myblog.utils.JsonResult;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/article")
public class ArticleController {

    @Resource
    private ArticleDao articleDao;

    @Resource
    private ArticleService articleService;

    @GetMapping
    public MultiPageInfo<Article> getArticleList(Integer aid,
                                                 @RequestParam(required = false) String keyWord,
                                                 @RequestParam(defaultValue = "1") Integer page,
                                                 @RequestParam(defaultValue = "10") Integer size) {
        return articleService.getArticleList(aid, keyWord, page, size);
    }

    @GetMapping("/type/{type}")
    public MultiPageInfo<Article> getArticleListByType(Integer aid,
                                                       @PathVariable(required = false) String type,
                                                       @RequestParam(defaultValue = "1") Integer page,
                                                       @RequestParam(defaultValue = "10") Integer size) {
        return articleService.getArticleListByType(aid, type, page, size);
    }

    @GetMapping("drafts")
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public MultiPageInfo<Article> getDraftsList(@RequestParam Integer aid,
                                                @RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer size) {
        return articleService.getDraftsArticleList(aid,page,size);
    }

    @GetMapping("cloud")
    public List<String> getDescTypeList() {
        return articleDao.getDescTypeList();
    }

    @GetMapping("/{id}")
    public Article getArticleContent(@PathVariable Integer id){
        var art = articleDao.getArticleContentById(id);
        List<String> type = articleDao.getTypeById(id);
        art.setType(type);
        if (art == null) throw new JsonException(404, "文章不存在");
        return art;
    }

    @PostMapping
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void addArticle(@RequestBody @Validated Article article, HttpSession session) {
        articleService.addArticle(article, session);
    }

    @PutMapping
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void updateArticle(@RequestBody(required = false) @Validated Article article, HttpSession session) { articleService.updateArticle(article,session); }

    @DeleteMapping
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void deleteArticle(@RequestBody @Validated Article article, HttpSession session) { articleService.deleteArticle(article,session); }

    @DeleteMapping("batchDelete")
    @Authentication(role = {UserRole.USER, UserRole.ADMIN})
    public void batchDeleteArticle(@RequestBody ArrayList<Integer> list) {
        articleService.batchDeleteArticle(list);
    }

    @GetMapping("maxId")
    public int getMaxArticleId() {
        return articleDao.getMaxId();
    }

    @PostMapping("cover")
    @Authentication(role = {UserRole.USER,UserRole.ADMIN})
    public JsonResult uploadArticleCover(@RequestBody MultipartFile file, Integer id) {
        articleService.setCover(id,file);
        return JsonResult.getInstance();
    }

    @PostMapping("articleImage")
    @Authentication(role = {UserRole.USER,UserRole.ADMIN})
    public JsonResult uploadArticleImage(@RequestBody MultipartFile file, Integer id) throws IOException {
        articleService.setArticleImage(id,file);
        return JsonResult.getInstance();
    }

    @GetMapping("cover")
    public HttpEntity<org.springframework.core.io.Resource> getCover(@RequestParam Integer id,
                                                                     @RequestParam String suffix,
                                                                     HttpServletResponse response)
            throws IOException {
        String fileName = id + "." + suffix;
        UrlResource resource = articleService.getArticleCoverResource(id,fileName);
        if (resource == null) {
            // return 响应默认封面 封面不存在
            response.sendRedirect("/api/assets/defaultCover.jpg");
            return null;
        }
        // return 响应正常头像
        return FileUtils.getFileResponseEntity(resource);
    }

    @GetMapping("cover/{id}")
    public HttpEntity<org.springframework.core.io.Resource> getCover(@PathVariable Integer id,
                                                                     HttpServletResponse response)
            throws IOException {
        UrlResource resource = articleService.getArticleCoverResource(id);
        if (resource == null) {
            // return 响应默认封面 封面不存在
            response.sendRedirect("/api/assets/defaultCover.jpg");
            return null;
        }
        // return 响应正常头像
        return FileUtils.getFileResponseEntity(resource);
    }

    @GetMapping("articleImage")
    public HttpEntity<org.springframework.core.io.Resource> getArticleImage (@RequestParam String fileName,
                                                                             @RequestParam Integer id) {
        UrlResource resource = articleService.getArticleImageResource(id,fileName);
        if (resource == null) {
            // return 图片不存在
            return null;
        }
        // return 响应图片
        return FileUtils.getFileResponseEntity(resource);
    }

    @DeleteMapping("articleImage")
    @Authentication(role = {UserRole.USER,UserRole.ADMIN})
    public void deleteArticleImage(@RequestParam String fileName, @RequestParam Integer id) {
        articleService.deleteArticleImage(fileName,id);
    }

    @DeleteMapping("cover")
    @Authentication(role = {UserRole.USER,UserRole.ADMIN})
    public void deleteArticleCover(@RequestParam String fileName) {
        articleService.deleteArticleCover(fileName);
    }

    @PostMapping("drafts")
    @Authentication(role = {UserRole.USER,UserRole.ADMIN})
    public void saveArticle(@RequestBody @Validated Article article, HttpSession session) {
        articleService.saveArticle(article,session);
    }

    @PostMapping("test")
    public void test(@RequestBody @Validated Article article) {
        System.out.println(article.getAbstract0() + " 为空");
    }

    @GetMapping("up/{id}")
    public int thumbsUp(@PathVariable Integer id) {
        articleDao.thumbsUp(id);
        return articleDao.getLikes(id);
    }

    @GetMapping("date/{id}")
    @Authentication(role = {UserRole.USER,UserRole.ADMIN})
    public Date getLastUpdateTime(@PathVariable Integer id) {
        return articleDao.getLastUpdateTime(id);
    }

    @GetMapping("sum/{id}")
    @Authentication(role = {UserRole.USER,UserRole.ADMIN})
    public int sumLikes(@PathVariable Integer id) {
        return articleDao.sumLikes(id);
    }
}
